package com.ruoyi.tongyan.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.tongyan.domain.TDeclarationProject;
import com.ruoyi.tongyan.mapper.TProjectMapper;
import com.ruoyi.tongyan.domain.TProject;
import com.ruoyi.tongyan.service.ITProjectService;

/**
 * 立项管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-28
 */
@Service
public class TProjectServiceImpl implements ITProjectService 
{
    @Autowired
    private TProjectMapper tProjectMapper;

    /**
     * 查询立项管理
     * 
     * @param projectId 立项管理主键
     * @return 立项管理
     */
    @Override
    public TProject selectTProjectByProjectId(Long projectId)
    {
        return tProjectMapper.selectTProjectByProjectId(projectId);
    }

    /**
     * 查询立项管理列表
     * 
     * @param tProject 立项管理
     * @return 立项管理
     */
    @Override
    public List<TProject> selectTProjectList(TProject tProject)
    {
        return tProjectMapper.selectTProjectList(tProject);
    }

    /**
     * 新增立项管理
     * 
     * @param tProject 立项管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTProject(TProject tProject)
    {
        int rows = tProjectMapper.insertTProject(tProject);
        insertTDeclarationProject(tProject);
        return rows;
    }

    /**
     * 修改立项管理
     * 
     * @param tProject 立项管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTProject(TProject tProject)
    {
        tProjectMapper.deleteTDeclarationProjectByProjectId(tProject.getProjectId());
        insertTDeclarationProject(tProject);
        return tProjectMapper.updateTProject(tProject);
    }

    /**
     * 批量删除立项管理
     * 
     * @param projectIds 需要删除的立项管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTProjectByProjectIds(Long[] projectIds)
    {
        tProjectMapper.deleteTDeclarationProjectByProjectIds(projectIds);
        return tProjectMapper.deleteTProjectByProjectIds(projectIds);
    }

    /**
     * 删除立项管理信息
     * 
     * @param projectId 立项管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTProjectByProjectId(Long projectId)
    {
        tProjectMapper.deleteTDeclarationProjectByProjectId(projectId);
        return tProjectMapper.deleteTProjectByProjectId(projectId);
    }

    /**
     * 新增申报和项目关联信息
     * 
     * @param tProject 立项管理对象
     */
    public void insertTDeclarationProject(TProject tProject)
    {
        List<TDeclarationProject> tDeclarationProjectList = tProject.getTDeclarationProjectList();
        Long projectId = tProject.getProjectId();
        if (StringUtils.isNotNull(tDeclarationProjectList))
        {
            List<TDeclarationProject> list = new ArrayList<TDeclarationProject>();
            for (TDeclarationProject tDeclarationProject : tDeclarationProjectList)
            {
                tDeclarationProject.setProjectId(projectId);
                list.add(tDeclarationProject);
            }
            if (list.size() > 0)
            {
                tProjectMapper.batchTDeclarationProject(list);
            }
        }
    }
}
